package com.kitty.game.gm.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.player.PlayerService;
import com.kitty.game.role.model.Role;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GmMail implements GMFather {

    @Override
    public boolean handle(Role aaaaa, String content) {
        String[] strings = content.split("##");
        Dao dao = SpringUtils.getBean(Dao.class);
        String sqlstr = "select uid from t_role where level >=30";
        Sql sql = Sqls.create(sqlstr);
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                ArrayList<Long> arrayList = new ArrayList();
                while (resultSet.next()) {
                    Long uid = resultSet.getLong("uid");
                    arrayList.add(uid);
                }
                return arrayList;
            }
        });
        dao.execute(sql);
        List<Long> list = sql.getList(Long.class);
        PlayerService playerService = SpringUtils.getPlayerService();
        for (Long integer : list) {
            Role aaaaaa = playerService.getPlayerBy(integer);
            if (aaaaaa == null) {
                continue;
            }
            SpringUtils.getMailService().buchang(aaaaaa, strings[0], Integer.parseInt(strings[1]));
        }
        return true;
    }
}
